"use strict";
cc._RF.push(module, '2ce7eZRKctDqahv1cA4ANS6', 'ManageCenter');
// scripts/mainScene/ManageCenter.js

'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

/**
 * 主界面控制中心
 */
cc.Class({
    extends: cc.Component,

    properties: {
        selectStateSp: cc.Node, //选中状态图片
        spPageBase: cc.SpriteFrame, //基本背景图
        spLotteryIconList: [cc.SpriteFrame], //彩种图片列表
        spLotteryNameList: [cc.SpriteFrame], //彩种名字列表
        pvActivity: cc.PageView, //活动幻灯片界面
        fbactivityPop: cc.Prefab, //活动界面
        fbLoginPop: cc.Prefab, //登录界面
        fbService: cc.Prefab, //客服
        labNot: cc.Label, //跑马灯
        curTg: cc.Toggle, //当前页
        tgGroups: [cc.Toggle],
        fbbet: cc.Prefab,
        sptest: cc.Node,

        _updateTimer: 0,
        _updateInterval: 3,
        _imgNums: 0, //幻灯片计数
        _coning: false,
        _reCount: 0, //重连次数
        _bFrist: true, //首次登陆
        _paoList: [], //跑马灯列表
        _playNum: 0,
        _serverTime: 0 //服务器时间

    },

    onLoad: function onLoad() {
        //设置的当前页面为 1
        CL.MAINMANAGE = this.node.getComponent(this.node.name);
        this.pvActivity.setCurrentPageIndex(0);
        this.labNot.node.x = -900;

        this.lottyerIconGruop = {
            '40010': 0, //幸运28图片
            '40020': 1, //加拿大28图片
            '301': 2, //老时时彩图片 
            '202': 3 //老11选5图片 
        };
        this.lottyerNameGruop = {
            '40010': 0, //幸运28
            '40020': 1, //加拿大28
            '301': 2, //老时时彩
            '202': 3 //老11选5 
        };

        this.schedule(this.upServerTime, 10);
        this.schedule(this.upTime, 1);
        this._serverTime = CL.NET.getServerTime();
    },

    upTime: function upTime() {
        if (this._upTime == 1) {
            this._serverTime += 1;
            //cc.log("time update时间："+this._serverTime);
        }
        this._upTime = 1;
    },


    getServerTime: function getServerTime() {
        return this._serverTime;
    },

    upServerTime: function upServerTime() {
        if (CL.NET.isConnect()) {
            this._hpConState = false;
            this._upTime = 0;
            this._serverTime = CL.NET.getServerTime();
            // cc.log("time net获取服务器时间："+this._serverTime);
        } else {
            var recv = function (ret) {
                if (ret.Code === 0) {
                    this._upTime = 0;
                    this._serverTime = parseInt(ret.ServerTime / 1000);
                    // cc.log("time http获取服务器时间："+this._serverTime);
                    if (!CL.NET.isConnect()) {
                        cc.log("chat disconnect reconnect");
                        this.onConnectSK();
                    }
                }
            }.bind(this);
            var data = {
                Token: User.getLoginToken()
            };
            CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETSERVERTIME, data, recv.bind(this), "POST");
        }
    },

    loadImg: function loadImg(data) {
        if (data.length > 0) {
            this.conNode = new cc.Node("page");
            this.sp = this.conNode.addComponent(cc.Sprite);
            var bt = this.conNode.addComponent(cc.Button);

            var clickEvents = new cc.Component.EventHandler();
            clickEvents.target = this;
            clickEvents.component = "ManageCenter";
            clickEvents.handler = "pageCallback";
            clickEvents.customEventData = data[this._imgNums].page;
            bt.clickEvents[0] = clickEvents;
            this.pvActivity.addPage(this.conNode);

            //动态加载图片
            // var url = data[this._imgNums].ad;
            // cc.log("init page:"+url);
            // if(url != "" && url != null &&typeof(url) != "undefined"){
            //     cc.log("init page: logo2:"+url);
            //     cc.loader.load({url, type:"jpg"}, function(err, tex){
            //         cc.log("init page: logo3:"+tex);
            //         if(tex != null){
            //             this._imgNums++;
            //             var sf = new cc.SpriteFrame(tex);
            //             this.sp.spriteFrame = sf;
            //             this.sp.node.width = 992;
            //             this.sp.node.height = 336;
            //         }
            //         else
            //         {
            //             this._imgNums++;
            //             this.sp.spriteFrame = this.spPageBase;
            //             this.sp.node.width = 992;
            //             this.sp.node.height = 336;
            //         }
            //         if(this._imgNums < data.length)
            //             this.loadImg(data);
            //     }.bind(this));
            // }
            // else
            // {
            this._imgNums++;
            this.sp.spriteFrame = this.spPageBase;
            this.sp.node.width = 992;
            this.sp.node.height = 336;
            if (this._imgNums < data.length) this.loadImg(data);
            //}
        } else {
            this.conNode = new cc.Node("page");
            this.sp = this.conNode.addComponent(cc.Sprite);
            var bt = this.conNode.addComponent(cc.Button);
            this.sp.spriteFrame = this.spPageBase;
            this.sp.node.width = 992;
            this.sp.node.height = 336;
            this.pvActivity.addPage(this.conNode);
        }
    },

    pageCallback: function pageCallback(event, customEventData) {
        if (Utils.isEffective(customEventData)) {
            var canvas = cc.find("Canvas");
            var activityPop = cc.instantiate(this.fbactivityPop);
            activityPop.getComponent(activityPop.name).init(customEventData);
            canvas.addChild(activityPop);
        }
    },

    onTgSelect: function onTgSelect(toggle, customEventData) {
        if (toggle == null) {
            toggle = this.tgGroups[customEventData];
        }

        if (toggle == null) return;

        if (toggle === this.curTg) {
            toggle.getComponent(cc.Toggle).isChecked = true;
            return;
        }

        switch (customEventData) {
            case '0':
                {
                    this.pvActivity.node.active = true;
                    this.labNot.node.parent.parent.active = true;
                    toggle.getComponent(cc.Toggle).isChecked = true;
                    this.curTg.getComponent(cc.Toggle).isChecked = false;
                    this.curTg = toggle;

                    var index = parseInt(customEventData);
                    var action = cc.moveTo(0.1, cc.p(-429 + index * 290, 0));
                    this.selectStateSp.runAction(action);
                }
                break;
            case '1':
                {
                    this.pvActivity.node.active = false;
                    this.labNot.node.parent.parent.active = false;
                    toggle.getComponent(cc.Toggle).isChecked = true;
                    this.curTg.getComponent(cc.Toggle).isChecked = false;
                    this.curTg = toggle;

                    var index = parseInt(customEventData);
                    var action = cc.moveTo(0.1, cc.p(-429 + index * 290, 0));
                    this.selectStateSp.runAction(action);
                    window.Notification.emit("updateRechargeUI", "");
                }
                break;
            case '2':
                {
                    this.pvActivity.node.active = true;
                    this.labNot.node.parent.parent.active = true;
                    toggle.getComponent(cc.Toggle).isChecked = true;
                    this.curTg.getComponent(cc.Toggle).isChecked = false;
                    this.curTg = toggle;

                    var index = parseInt(customEventData);
                    var action = cc.moveTo(0.1, cc.p(-429 + index * 290, 0));
                    this.selectStateSp.runAction(action);
                }
                break;
            case '3':
                {
                    if (User.getIsvermify()) //验证通过
                        {
                            this.pvActivity.node.active = false;
                            this.labNot.node.parent.parent.active = false;
                            toggle.getComponent(cc.Toggle).isChecked = true;
                            this.curTg.getComponent(cc.Toggle).isChecked = false;
                            this.curTg = toggle;

                            var index = parseInt(customEventData);
                            var action = cc.moveTo(0.1, cc.p(429, 0));
                            this.selectStateSp.runAction(action);
                            window.Notification.emit("updateUserUI", "");
                            return;
                        } else {
                        toggle.getComponent(cc.Toggle).isChecked = false;
                        this.curTg.getComponent(cc.Toggle).isChecked = true;
                        var callback = function callback(ret) {
                            if (ret && ret == true) {
                                this.pvActivity.node.active = false;
                                this.labNot.node.parent.parent.active = false;
                                toggle.getComponent(cc.Toggle).isChecked = true;
                                this.curTg.getComponent(cc.Toggle).isChecked = false;
                                this.curTg = toggle;

                                var index = parseInt(customEventData);
                                var action = cc.moveTo(0.1, cc.p(429, 0));
                                this.selectStateSp.runAction(action);
                                window.Notification.emit("updateUserUI", "");
                                return;
                            }
                        };
                        this.goToLogin(callback.bind(this));
                    }
                }
                break;
            default:
                cc.error("managecenter error");
                break;
        }
    },

    //通过彩种id获取彩种icon
    getLotteryIconById: function getLotteryIconById(lottyID) {
        var id = this.lottyerIconGruop[lottyID];
        return this.spLotteryIconList[id];
    },

    //通过彩种id获取彩种名字sprite
    getLotteryNameById: function getLotteryNameById(lottyID) {
        var id = this.lottyerNameGruop[lottyID];
        return this.spLotteryNameList[id];
    },

    // page监听事件
    onPageEvent: function onPageEvent(sender, eventType) {
        //翻页事件
        if (eventType !== cc.PageView.EventType.PAGE_TURNING) {
            return;
        }
    },

    //前往登录
    goToLogin: function goToLogin(callback) {
        var canvas = cc.find("Canvas");
        var loginpop = cc.instantiate(this.fbLoginPop);
        loginpop.getComponent(loginpop.name).setCallBack(callback);
        canvas.addChild(loginpop);
    },

    //客服
    onService: function onService() {
        var canvas = cc.find("Canvas");
        var fbs = cc.instantiate(this.fbService);
        canvas.addChild(fbs);
    },

    update: function update(dt) {
        this._updateTimer += dt;
        if (this._updateTimer > this._updateInterval) //3秒执行一次活动页
            {
                this._updateTimer = 0;
                var pageIndex = this.pvActivity.getCurrentPageIndex() + 1 == this.pvActivity.getPages().length ? 0 : this.pvActivity.getCurrentPageIndex() + 1;
                this.pvActivity.scrollToPage(pageIndex);
            }

        var playing = true;
        var x = this.labNot.node.x;
        x -= dt * 100;
        if (x < -900) {
            x = 900;
            playing = false;
        }

        this.labNot.node.x = x;

        if (playing) return;

        //跑马灯
        if (this._playNum > 0) {
            this._playNum--;
            return;
        }
        if (this._playNum == 0 && this._paoList.length > 0) {
            var item = this._paoList.pop();
            this._playNum = item.Number;
            this.labNot.string = item.Content;
            this._playNum--;
            return;
        }

        if (this._paoList.length == 0) {
            this.labNot.string = "买彩票，中大奖，欢迎来到PC蛋蛋。";
        }
    },

    //解析聊天类的内容
    pushEvent: function pushEvent(data) {
        this.arData = [];
        if (Object.prototype.toString.call(data) !== "[object Array]") {
            this.arData.push(data);
        } else this.arData = data;

        if (this._Pushing) return;

        var dataTemp = this.arData.slice();
        for (var i = 0; i < dataTemp.length; i++) {
            this._Pushing = true;
            var oneContent = dataTemp[i];
            var contentobj = "";
            var obj = "";
            var curTime = "";

            if ((typeof oneContent === 'undefined' ? 'undefined' : _typeof(oneContent)) !== 'object') {
                try {
                    contentobj = eval('(' + oneContent + ')');
                } catch (error) {
                    return;
                }
            } else contentobj = oneContent;
            try {
                obj = eval('(' + contentobj.data + ')');
            } catch (error) {
                return;
            }

            curTime = contentobj.time;

            var data0 = {
                data: obj,
                time: curTime
            };

            switch (obj.MsgType) {
                case DEFINE.RECV_MSG_TYPE.MSG_TYPE_SYS:
                    {
                        window.Notification.emit("sysmsg_push", data0);
                    }
                    break;
                case DEFINE.RECV_MSG_TYPE.MSG_TYPE_USER:
                    {
                        window.Notification.emit("newmsg_push", data0);
                    }
                    break;
                case DEFINE.RECV_MSG_TYPE.MSG_TYPE_PAO:
                    {
                        //cc.log("chat 跑马灯");
                        window.Notification.emit("rPao_push", data0.data);
                    }
                    break;
                case DEFINE.RECV_MSG_TYPE.MSG_TYPE_OPEN:
                    {
                        //cc.log("chat 开奖列表刷新");
                        window.Notification.emit("rOpen_push", data0);
                    }
                    break;
                case DEFINE.RECV_MSG_TYPE.MSG_TYPE_LOTSTATE:
                    //同步彩种状态
                    {
                        window.Notification.emit("rLotState_push", data0);
                    }
                    break;
                default:
                    break;
            }
        }
        this._Pushing = false;
    },

    //注册服务器返回信息事件
    initHandlers: function initHandlers() {
        var self = this;
        CL.NET.addHandler("newmsg", function (data) {
            self.pushEvent(data);
        });

        CL.NET.addHandler("sysmsg", function (data) {
            self.pushEvent(data);
        });

        CL.NET.addHandler("otherjoin", function (data) {
            //加入房间返回
            window.Notification.emit("otherjoin_push", data);
        });

        CL.NET.addHandler("rleave", function (data) {
            //离开房间
            cc.log("chat rleave:" + data.data);
        });

        CL.NET.addHandler("rlogin", function (data) {
            //登录返回
            var obj = data;
            if (cc.sys.isNative) {
                if (Object.prototype.toString.call(data) !== "object") {
                    try {
                        obj = eval('(' + data + ')');
                    } catch (error) {
                        window.Notification.emit("rlogin_push", false);
                        return;
                    }
                }
            }

            if (obj.code == 0) //token效验失败
                {
                    CL.NET.close();
                    window.Notification.emit("rlogin_push", false);
                } else if (obj.code == 1) {
                CL.NET.sio.connected = true;
                window.Notification.emit("rlogin_push", true);
            } else if (obj.code == 2) //后端未通知登录事件
                {
                    CL.NET.close();
                    window.Notification.emit("rlogin_push", false);
                }
        });

        CL.NET.addHandler("rroomlist", function (data) {
            window.Notification.emit("rroomlist_push", data);
        }.bind(this));

        //跑马灯
        window.Notification.on('rPao_push', function (data) {
            console.log("chat rPao_push");
            this._paoList.push(data);
        }.bind(this));
    },

    onLoginSK: function onLoginSK() {
        if (CL.NET.isConnect()) {
            CL.NET.login();
        } else {
            cc.log("chat is disconnect can't loginsk");
            this.onConnectSK();
        }
    },

    //连接服务器
    onConnectSK: function onConnectSK() {
        cc.log("chat onConnectSK");
        if (this._bFrist) {
            this._bFrist = false;
            this.initHandlers();
        }

        if (CL.NET.isConnect()) {
            cc.log("chat onconnectsk isconnect");
            return;
        }
        if (this._coning) {
            cc.log("chat onconnectsking");
            return;
        } else {
            cc.log("chat onConnectSK 1");
            var self = this;
            var onConnectOK = function onConnectOK() {
                self._reCount = 0;
                self._coning = false;
                cc.log("chat onConnectOK");
                CL.NET.login();
            };
            var onConnectFailed = function onConnectFailed(data) {
                cc.log("chat onConnectFailed");
                self._coning = false;
            };
            CL.NET.connect(onConnectOK, onConnectFailed);
            this._coning = true;
        }
    },

    //重连
    reConnectSK: function reConnectSK() {
        cc.log("chat reConnectSK");
        if (CL.NET.isConnect()) {
            cc.log("chat reConnectSK isconnect");
            return;
        }
        if (this._reCount >= 3) {
            cc.log("chat reConnectSK recount MAX");
            return;
        }

        this._reCount++;
        cc.log("chat reconnect :" + this._reCount);
        this.onConnectSK();
    }

});

cc._RF.pop();